<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
			<div class="app-space-between">
				<div class="app-space-between-left">
					<el-form-item label="姓名:" prop="name">
						<el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @clear="handleQuery" />
					</el-form-item>
					<el-form-item label="手机号:" prop="phone">
						<el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @clear="handleQuery" />
					</el-form-item>
					<el-form-item label="台餐卡号:" prop="mealCardNumber">
						<el-input v-model="queryParams.mealCardNumber" placeholder="请输入台餐卡号" clearable
							@clear="handleQuery" />
					</el-form-item>

					<el-form-item label="状态:" prop="state">
						<el-select v-model="queryParams.state" placeholder="请选择状态" @change="handleQuery">
							<el-option v-for="item in couponTypeEnum" :key="item.value" :label="item.label"
								:value="item.value" />
						</el-select>
					</el-form-item>

					<el-form-item label="充值时间:" prop="getCouponTimeList">
						<el-date-picker v-model="getCouponTimeList" type="daterange" @change="handleQuery"
							start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
						</el-date-picker>
					</el-form-item>

				</div>
				<div class="app-space-between-right">
					<el-form-item>
						<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
						<el-button size="mini" @click="resetQuery">重置</el-button>
					</el-form-item>
				</div>
			</div>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleImport">导入</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleExport">导出</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="multiple" @click="handleMoreRecharge">批量充值
				</el-button>
			</el-col>

			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
			<el-table-column show-overflow-tooltip type="selection" width="55" align="center" />
			<el-table-column show-overflow-tooltip label="姓名" width="120" align="center" prop="name" />
			<el-table-column show-overflow-tooltip label="手机号" width="200" align="center" prop="phone" />
			<el-table-column show-overflow-tooltip label="台餐卡号" width="180" align="center" prop="mealCardNumber" />
			<el-table-column show-overflow-tooltip label="积分" min-width="120" align="center" prop="integral" />
			<el-table-column show-overflow-tooltip label="状态" min-width="80" align="center" prop="state">
				<template slot-scope="scope">
					{{Number(scope.row.state) == 0 ? '冻结账户' : '正常账户'}}
				</template>
			</el-table-column>
			<el-table-column show-overflow-tooltip label="创建时间" min-width="200" align="center" prop="creatorTime">
				<template slot-scope="scope">
					{{scope.row.creatorTime | $YYYY_MM_DD_hh_mm_ss}}
				</template>
			</el-table-column>
			<el-table-column show-overflow-tooltip label="操作" align="center" width="260" fixed="right">
				<template slot-scope="scope">
					<el-link type="primary" class="brother" :underline="false" @click="handleUpdate(scope.row)">编辑
					</el-link>
					<el-link type="primary" class="brother" :underline="false" @click="handleDelete(scope.row)">删除
					</el-link>
					<el-link type="primary" class="brother" v-if="Number(scope.row.state) == 0" :underline="false" @click="handleLock(scope.row,1)">解冻
					</el-link>
					<el-link type="primary" class="brother" v-if="Number(scope.row.state) == 1" :underline="false" @click="handleLock(scope.row,0)">冻结
					</el-link>
					<el-link type="primary" class="brother" :underline="false" @click="handleRecharge(scope.row)">积分充值
					</el-link>
					<el-link type="primary" class="brother" :underline="false" @click="handleRechargeLogs(scope.row)">
						积分记录</el-link>
				</template>
			</el-table-column>
		</el-table>
		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 新增 修改 -->
		<people-add ref="people-add" @success="handleQuery"></people-add>
		<!-- 单条数据积分充值 -->
		<people-recharge ref="people-recharge" @success="handleQuery"></people-recharge>
		<!-- 多条数据积分充值 -->
		<people-more-recharge ref="people-more-recharge" @success="handleQuery"></people-more-recharge>
		<!-- 积分记录 -->
		<people-recharge-logs ref="people-recharge-logs"></people-recharge-logs>
		<!-- 导入 -->
		<import-xlsx ref="import-xlsx" templateUrl="/system/information/importTemplate"
			url="/system/information/importData" @success="handleQuery"></import-xlsx>
	</div>
</template>

<script>
	import {
		listInformation,
		deleteInformation,
		updataInformation,
		exportInformation
	} from "@/api/veteranCadre/people.js";
	import peopleAdd from "./components/people-add.vue";
	import peopleRecharge from "./components/people-recharge.vue";
	import peopleMoreRecharge from "./components/people-more-recharge.vue";
	import peopleRechargeLogs from "./components/people-recharge-logs.vue";
	import importXlsx from "@/components/importXlsx";
	export default {
		components: {
			importXlsx,
			peopleAdd,
			peopleRecharge,
			peopleMoreRecharge,
			peopleRechargeLogs
		},
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 岗位表格数据
				postList: [],
				// 弹出层标题
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					name: '',
					phone: '',
					mealCardNumber: '',
					state: '',
					beginTime: '', 
					endTime: '', 
				},
				getCouponTimeList: [],
				couponTypeEnum: [{
					label: '冻结账户',
					value: '0'
				}, {
					label: '正常账户',
					value: '1'
				}]
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询岗位列表 */
			getList() {
					this.queryParams.beginTime = this.getCouponTimeList[0];
					this.queryParams.endTime = this.getCouponTimeList[1];
					this.loading = true;
				listInformation({}, this.queryParams).then(response => {
					this.postList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 导入模板 */
			handleImport(){
				this.$refs['import-xlsx'].init({
					title: "老干部信息管理"
				});
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.getCouponTimeList = [];
				this.resetForm("queryForm");
				this.handleQuery();
			},
			/** 多选框选中数据 */
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			/** 1解冻 0冻结 */
			async handleLock(options, state) {
				let content = '';
				let tips = `确认冻结账户“${options.name}”吗？`;
				if (state == 0) {
					content = '冻结账户后，不能进入老干部专区消费'
				}
				if (state == 1) {
					content = '解冻账户后，可进入老干部专区消费'
				};
				this.$confirm(content, tips, {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					return updataInformation({
						id: options.id,
						state
					});
				}).then(res=>{
					this.$message.success("操作成功");
					this.handleQuery();
				});
			},
			/** 积分充值 */
			handleRecharge(options) {
				this.$refs['people-recharge'].init();
			},
			/** 积分记录 */
			handleRechargeLogs(options) {
				this.$refs['people-recharge-logs'].init();
			},
			/** 批量充值 */
			handleMoreRecharge() {
				this.$refs['people-more-recharge'].init();
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.$refs['people-add'].init();
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				let postId = row.id || this.ids
				if (Array.isArray(postId)) postId = postId.toString();
				this.$refs['people-add'].init(postId);
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				let postIds = row.id || this.ids;
				if (Array.isArray(postIds)) postIds = postIds.toString();
				this.$confirm("删除后，信息不能恢复。", `确认删除${row.name}用户吗？`, {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					return deleteInformation(postIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			async handleExport() {
				this.download('/system/information/export', {
					...this.queryParams
				}, `veteran_cadre_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
<style lang="scss" scoped>
	.brother+.brother {
		margin-left: 6px;
	}
</style>
